﻿using Microsoft.Web.WebView2.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CADIfox_MVVM.View
{
    /// <summary>
    /// ThreeView.xaml 的交互逻辑
    /// </summary>
    public partial class ThreeView : Window
    {
        public ThreeView()
        {
            InitializeComponent();
            InitializeAsync();
        }
        async void InitializeAsync()
        {
            await webView.EnsureCoreWebView2Async(null);
            //接收消息
            webView.CoreWebView2.WebMessageReceived += UpdateAddressBar;
        }

        void UpdateAddressBar(object sender, CoreWebView2WebMessageReceivedEventArgs args)
        {
            String uri = args.TryGetWebMessageAsString();
            System.Windows.Forms.MessageBox.Show("您正在接受同事的返回信息" + uri);
            webView.CoreWebView2.PostWebMessageAsString(uri);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            webView.CoreWebView2.PostWebMessageAsString("hello world");

            string js_value = "abc";
            webView.CoreWebView2.ExecuteScriptAsync($"alert('收到C#要求执行的js函数，弹窗显示js_value：{js_value}')");
        }
    }
}
